package com.xuyu.nacos.callback.listener.analysekey.onmethod;

import com.alibaba.nacos.api.config.ConfigChangeItem;
import com.xuyu.nacos.callback.listener.analysekey.AbstractGetKey;
import com.xuyu.nacos.callback.listener.annoation.NacosListenerCallBackMethod;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Objects;

/**
 * @author xuyu
 * 处理带了NacosListenerCallBackMethod注解的
 */
@Component
public class WithCallBackMethodAnnotation extends AbstractGetKey<Method> {

    @Override
    public boolean choose(Method method) {
        return method.isAnnotationPresent(NacosListenerCallBackMethod.class) && selfDefinedMethodCheck(method);
    }

    @Override
    public String getKey(Method method) {
        NacosListenerCallBackMethod annotation = method.getAnnotation(NacosListenerCallBackMethod.class);
        return annotation.key();
    }


    private boolean selfDefinedMethodCheck(Method method) {
        Parameter[] parameters = method.getParameters();
        if (parameters.length != 1) {
            return false;
        }

        return Objects.equals(parameters[0].getType(), ConfigChangeItem.class);
    }
}
